import Vue from "vue";
import Router from "vue-router"
import store from "../store"


// 路由懒加载
const Login = () => import(/* webpackChunkName: "Login_Home_Welcome_ResetPwd" */ '../view/Login.vue')
const Home = () => import(/* webpackChunkName: "Login_Home_Welcome_ResetPwd" */ '../view/Home.vue')
const Welcome = () => import(/* webpackChunkName: "Login_Home_Welcome_ResetPwd" */ '../view/personal/Welcome.vue')
const ResetPwd = () => import(/* webpackChunkName: "Login_Home_Welcome_ResetOwd" */ '../view/personal/ResetPwd.vue')

const Users = () => import(/* webpackChunkName: "Users" */ '../view/users/User.vue')

const Roles = () => import(/* webpackChunkName: "Auth" */ '../view/authority/Roles.vue')
const Menus = () => import(/* webpackChunkName: "Auth" */ '../view/authority/Menus.vue')
const API = () => import(/* webpackChunkName: "Auth" */ '../view/authority/API.vue')

const BaseList = () => import(/* webpackChunkName: "System" */ '../view/system/Base.vue')
const ClassList = () => import(/* webpackChunkName: "System" */ '../view/system/Class.vue')
const Course = () => import(/* webpackChunkName: "System" */ '../view/system/Course.vue')

const StudentList = () => import(/* webpackChunkName: "Students" */ '../view/student/StudentList.vue')
const StudentInfo = () => import(/* webpackChunkName: "Students" */ '../view/student/StudentInfo.vue')
const StuActivity = () => import(/* webpackChunkName: "Students" */ '../view/student/StuActivity.vue')
const Research = () => import(/* webpackChunkName: "Students" */ '../view/student/Research.vue')
const Grants = () => import(/* webpackChunkName: "Students" */ '../view/student/Grants.vue')
const Rewards = () => import(/* webpackChunkName: "Students" */ '../view/student/Rewards.vue')
const Jobs = () => import(/* webpackChunkName: "Students" */ '../view/student/Jobs.vue')

// const ScoreImport = () => import(/* webpackChunkName: "Scores" */ '../view/score/ScoreImport.vue')
const UploadExcel = () => import(/* webpackChunkName: "Scores" */ '../view/score/upload-excel.vue')
const ScoreList = () => import(/* webpackChunkName: "Scores" */ '../view/score/ScoreList.vue')
const ScoreSummary = () => import(/* webpackChunkName: "Scores" */ '../view/score/ScoreSummary.vue')
const ScoreFilter = () => import(/* webpackChunkName: "Scores" */ '../view/score/ScoreFilter.vue')

const Archives = () => import(/* webpackChunkName: "Party" */ '../view/party/Archives.vue')
const PartyFilter = () => import(/* webpackChunkName: "Party" */ '../view/party/partyFilter.vue')

const Messages = () => import(/* webpackChunkName: "Message" */ '../view/message/Messages.vue')
const Notices = () => import(/* webpackChunkName: "Message" */ '../view/message/Notices.vue')
const Tasks = () => import(/* webpackChunkName: "Message" */ '../view/message/Tasks.vue')


const Error = () => import(/* webpackChunkName: "Error" */ '../view/404.vue')

// 注册路由
Vue.use(Router);

// 配置路由
const router = new Router({
    // mode: "history",
    routes: [
        { path: "/", redirect: "/home"},
        { path: "/Login", component: Login},
        {
            path: "/home",
            component: Home,
            redirect: "/welcome",
            children: [
                {path: "/welcome", component: Welcome},
                {path: "/reset_pwd", component: ResetPwd},
                {path: "/users", component: Users, meta: {requireAuth: true, title: "用户列表"}},
                {path: "/roles", component: Roles, meta: {requireAuth: true, title: "角色列表"}},
                {path: "/menus", component: Menus, meta: {requireAuth: true, title: "菜单列表"}},
                {path: "/api", component: API, meta: {requireAuth: true, title: "API列表"}},
                {path: "/base", component: BaseList, meta: {requireAuth: true, title: "基础配置"}},
                {path: "/class", component: ClassList, meta: {requireAuth: true, title: "班级列表"}},
                {path: "/course", component: Course, meta: {requireAuth: true, title: "课程管理"}},

                {path: "/students", component: StudentList, meta: {requireAuth: true, title: "学生列表"}},
                {path: "/studentInfo/:id",name: "studentInfo", component: StudentInfo, meta: {requireAuth: true, title: "学生信息"}},
                {path: "/stuActivity", component: StuActivity, meta: {requireAuth: true, title: "学生活动"}},
                {path: "/stuResearch", component: Research, meta: {requireAuth: true, title: "科研管理"}},
                {path: "/rewards", component: Rewards, meta: {requireAuth: true, title: "获奖经历"}},
                {path: "/grants", component: Grants, meta: {requireAuth: true, title: "助学金"}},
                {path: "/jobs", component: Jobs, meta: {requireAuth: true, title: "学生就业"}},

                // {path: "/scoreImport", component: ScoreImport, meta: {requireAuth: true, title: "成绩导入"}},
                {path: "/scoreImport", component: UploadExcel, meta: {requireAuth: true, title: "成绩导入"}},
                {path: "/scoreList", component: ScoreList, meta: {requireAuth: true, title: "成绩列表"}},
                {path: "/scoreSummary", component: ScoreSummary, meta: {requireAuth: true, title: "成绩汇总"}},
                {path: "/scoreFilter", component: ScoreFilter, meta: {requireAuth: true, title: "成绩筛选"}},

                {path: "/archives", component: Archives, meta: {requireAuth: true, title: "政治档案"}},
                {path: "/partyFilter", component: PartyFilter, meta: {requireAuth: true, title: "党员筛选"}},

                {path: "/messages", component: Messages, meta: {requireAuth: true, title: "消息列表"}},
                {path: "/notices", component: Notices, meta: {requireAuth: true, title: "通知列表"}},
                {path: "/tasks", component: Tasks, meta: {requireAuth: true, title: "任务列表"}},

                {path: "/404", component: Error}
            ]
        },
        { path: "*", redirect: "/404"},
    ]
});

router.beforeEach((to, from, next) => {
    if (to.path === "/Login") return next()
    if (to.meta.requireAuth){
        if (!store.getters.menu_path_map.hasOwnProperty(to.path) ) {
            if (!store.getters.menu_path_map.hasOwnProperty("/" + to.name)) {
                return next('/404')
            }
        }
    }
    const tokenStr = window.sessionStorage.getItem("token");
    if (!tokenStr) return next("/Login")
    next()
})

export default router